// 主平台通信桥
import { switchTheme } from '../utils/tools';

export default class Bridge {
  static tileId = null;
  static token = null;
  static _initialized = false;
  /**
   * 初始化通信桥，监听主平台指令
   */
  static init(tileId) {
    if (this._initialized) return;
    this._initialized = true;
    this.tileId = tileId;
    this.send('ready');
    console.log('[bridge] 初始化通信桥');
  };
  static send = (command, data) => {
    window.parent.postMessage({
        type: command,
        data: data,
        tileId: this.tileId,
        source: 'widget'
    }, "*");
  }
  static destroy = () => {
    if (!this._initialized) return;
    this._initialized = false;
  };
  static setToken = (token) => {
    this.token = token;
  }
}